package com.whxd.smarthome.core.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whxd.smarthome.core.entity.DhDevice;
import com.whxd.smarthome.core.entity.DhDeviceShare;
import com.whxd.smarthome.core.repository.DhDeviceDao;
import com.whxd.smarthome.core.repository.DhDeviceShareDao;

@Service
public class DhDeviceChannelServiceImpl implements DhDeviceShareService {
	
	@Autowired
	private DhDeviceShareDao dhDeviceShareDao;
	@Autowired
	private DhDeviceDao dhDeviceDao;
	
	@Override
	public void shareDevice(DhDevice device, String sharedtype, String beSharedTo) {
		DhDeviceShare dhDeviceShare = new DhDeviceShare();
		BeanUtils.copyProperties(device, dhDeviceShare);
		dhDeviceShare.setBeSharedFrom(device.getOwnerUsername());
		dhDeviceShare.setSharedType(sharedtype);//分享类型：square视频广场，personal分享给个人
		if ("square".equals(sharedtype)) {
			dhDeviceShare.setBeSharedTo("ALL");
		}else {
			dhDeviceShare.setBeSharedTo(beSharedTo);
		}
		dhDeviceShareDao.save(dhDeviceShare);
		device.setBeSharedTo(true);
		dhDeviceDao.save(device);
	}

	@Override
	public void cancelDeviceShared(String deviceId) {
		dhDeviceShareDao.delete(dhDeviceShareDao.getDeviceShare(deviceId));
		DhDevice device = dhDeviceDao.findByDeviceId(deviceId);
		device.setBeSharedTo(false);
		dhDeviceDao.save(device);
	}

	@Override
	public List<DhDeviceShare> getDeviceSquare() {
		return dhDeviceShareDao.getDeviceSquare();
	}


}
